﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ManagerClinic.Core.Common;
using ManagerClinic.Core.Business;
using ManagerClinic.Modules;
using ManagerClinic.Ultilities.Ultility;

namespace ManagerClinic.DanhMuc
{
    public partial class frmCapNhatDmBacSy : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmBacSyEvent;
        int State = 0;
        DmBacSyEntity dmBacSyEntity = null;

        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmKhoaBusiness dmKhoaBusiness = new DmKhoaBusiness();

        public frmCapNhatDmBacSy(int state)
        {
            InitializeComponent();
            this.State = state;
            LoadDmKhoa();
        }

        public frmCapNhatDmBacSy(int state, DmBacSyEntity obj)
        {
            InitializeComponent();

            this.State = state;
            LoadDmKhoa();
            dmBacSyEntity = obj;

            txtMaBacSy.Text = dmBacSyEntity.MaBacSy;
            txtTenBacSy.Text = dmBacSyEntity.TenBacSy;
            txtDiaChi.Text = dmBacSyEntity.DiaChi;
            txtDienThoai.Text = dmBacSyEntity.DienThoai;
            txtEmail.Text = dmBacSyEntity.Email;
            lkDmKhoa.EditValue = dmBacSyEntity.MaKhoa;
            chkTrangThaiSuDung.Checked = dmBacSyEntity.TrangThaiSuDung;
            txtMaBacSy.Enabled = false;
            txtTenBacSy.Focus();
        }

        void LoadDmKhoa()
        {
            var lstDmKhoaEntity = dmKhoaBusiness.GetDmKhoaList();
            lstDmKhoaEntity.Insert(0, new DmKhoaEntity() { MaKhoa = ConstantManagerClinic.sValueMember, TenKhoa = ConstantManagerClinic.sDisplayMember });
            lkDmKhoa.Properties.DataSource = lstDmKhoaEntity;
            lkDmKhoa.Properties.DisplayMember = "TenKhoa";
            lkDmKhoa.Properties.ValueMember = "MaKhoa";
            lkDmKhoa.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDataDmKhoa(object sender, EventArgs e)
        {
            LoadDmKhoa();
        }

        private void frmCapnhatDMBacSy_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmddmkhoa_Click(object sender, EventArgs e)
        {
            frmCapNhatDmKhoa frm = new frmCapNhatDmKhoa((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmKhoaEvent += new EventHandler(LoadDataDmKhoa);
            frm.ShowDialog();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkDmKhoa, "Khoa") == false) return;
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaBacSy, "Mã bác sỹ") == false) return;

            dmBacSyEntity = new DmBacSyEntity()
            {
                MaBacSy = txtMaBacSy.Text,
                TenBacSy = txtTenBacSy.Text.Trim(),
                DiaChi = txtDiaChi.Text.Trim(),
                DienThoai = txtDienThoai.Text.Trim(),
                Email = txtEmail.Text.Trim(),
                MaKhoa = lkDmKhoa.EditValue.ToString(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmBacSyBusiness.GetDmBacSy(txtMaBacSy.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaBacSy, "Mã phân loại") == false) return;

                dmBacSyBusiness.InsertDmBacSy(dmBacSyEntity);
                Validation.ClearControl(this);
                lkDmKhoa.Focus();
            }
            else
            {
                dmBacSyBusiness.EditDmBacSy(dmBacSyEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmBacSyEvent != null)
                dmBacSyEvent(sender, e);
        }

        private void frmCapnhatDMBacSy_Load(object sender, EventArgs e)
        {

        }

    }
}